#ifndef __CONTROLLER_H__
#define __CONTROLLER_H__
#include "gd32f10x.h"
#include "panel.h"
#define SOFT_VERSION_GIT    "SoftVer_1.1.4"
/* 版本号 */
#define SOFT_VERSION    0x10
/* 波特率 */
#define BAUDRATE_2400   1
#define BAUDRATE_4800   2
#define BAUDRATE_9600   3
/* 阀输出位 */
#define BIT_VALVE_AC    0x01
#define BIT_VALVE_DN    0x02
/* 电源控制位 */
#define BIT_POWER_AC    0x01
#define BIT_POWER_DN    0x02
#define BIT_POWER_FRESH 0x04
/* 空调模式选择位 */
#define BIT_ACMODE_COOL 0x01
#define BIT_ACMODE_HEAT 0x02
#define BIT_ACMODE_AIR  0x04
#define BIT_ACMODE_DRY  0x08
/* 空调模式 */
#define ACMODE_AIR          0
#define ACMODE_COOL         1
#define ACMODE_COOL_SLEEP   2
#define ACMODE_HEAT         3
#define ACMODE_HEAT_SLEEP   4
#define ACMODE_DRY          5
/* 系统选择 */
#define SYSTEM_AC       1   //空调
#define SYSTEM_DN       2   //地暖
#define SYSTEM_FRESH    3   //新风
#define SYSTEM_AC_DN    4   //地暖+空调
#define SYSTEM_AC_FRESH 5   //新风+空调
#define SYSTEM_DN_FRESH 6   //地暖+新风 
#define SYSTEM_FULL     7   //全机型
/* 当前页面 */
#define PAGE_SYSTEM     0   //系统页面
#define PAGE_ENGINEER   1   //工程师页面
#define PAGE_ADVANCE    2   //高级页面
#define PAGE_ERROR      3   //错误页面
/* 菜单 */
#define MENU_CALIB              1   
#define MENU_ECO                2  
#define MENU_SPEED_CTRL         3   
#define MENU_LOW_TEMP_PROTECT   4   
#define MENU_MEMORY             5   
#define MENU_BACKLASH_UP        6   
#define MENU_BACKLASH_DOWN      7   
#define MENU_TEMP_SET_LIMIT_UP  8   
#define MENU_TEMP_SET_LIMIT_DOWN 9   
#define MENU_COOL_ENABLE        10   
#define MENU_HEAT_ENABLE        11   
#define MENU_AIR_ENABLE         12   
#define MENU_DRY_ENABLE         13 
#define MENU_UNIT               14 
#define MENU_ADDRESS            15 
#define MENU_VERSION            51
#define MENU_BAUDRATE           52 
#define MENU_MACHINE_ENABLE     53
#define MENU_LUMINANCE          54 
#define MENU_RELAY_MODE         55
/* 风速设定 */
#define SPEED_SET_AUTO          0
#define SPEED_SET_LOW           1
#define SPEED_SET_MID           2
#define SPEED_SET_HIGH          3
/* 风速输出 */
#define SPEED_OUT_OFF           0
#define SPEED_OUT_LOW           1
#define SPEED_OUT_MID           2
#define SPEED_OUT_HIGH          3
/* 寄存器地址 */
#define REG_TEMP_SMP            0   //室内温度
#define REG_VALVE_STATE         1   //阀输出状态
#define REG_SPEED_OUT_AC        2   //空调风速输出
#define REG_SPEED_OUT_FRESH     3   //新风风速输出
#define REG_POWER_STATE         4   //电源状态
#define REG_MODE_AC             5   //空调模式
#define REG_TEMP_SET_AC         6   //空调设定温度
#define REG_TEMP_SET_DN         7   //地暖设定温度
#define REG_SPEED_SET_AC        8   //空调设定风速
#define REG_SPEED_SET_FRESH     9   //新风设定风速
#define REG_BTN_LOCK            10  //童锁
#define REG_LOW_TEMP_PROTECT    11  //低温保护
#define REG_ECO                 12  //节能模式
#define REG_SPEED_CTRL          13  //风速受控
#define REG_MEMORY              14  //记忆模式
#define REG_CALIB               15  //温度校准
#define REG_BACKLASH_UP         16  //回差上限
#define REG_BACKLASH_DOWN       17  //回差下限
#define REG_TEMP_SET_LIMIT_UP   18  //设定温度上限
#define REG_TEMP_SET_LIMIT_DOWN 19  //设定温度下限
#define REG_ACMODE_SELECT       20  //空调模式选择
#define REG_TEMP_UNIT           21  //温度单位
#define REG_SYSTEM              22  //当前系统
#define REG_SYSTEM_SELECT       23  //机型选择
#define REG_NUM                 24
/* 风扇延时关闭 */
#define SPEED_OUT_FOREVER       (0xFFFFFFFF)
#define SPEED_OUT_DELAY         (60)
/* 节能模式下温度上下限 */
#define ECO_TEMP_SET_LIMIT_UP   (20)
#define ECO_TEMP_SET_LIMIT_DOWN (26)
/* 按键背光 */
#define BTN_LIGHT_POWER         0
#define BTN_LIGHT_PAGE          1 
#define BTN_LIGHT_OTHER         2
/* 继电器模式 */
#define RELAY_MODE_AC           1
#define RELAY_MODE_DN_FRESH     0
/* 温度校准上下限 */
#define CALIB_TEMP_LIMIT_UP     (8)     //温度校准上限
#define CALIB_TEMP_LIMIT_DOWN   (-8L)   //温度校准下限
/* 回差上限上下限 */
#define BACKLASH_UP_LIMIT_UP    (6)     //回差上限最大值
#define BACKLASH_UP_LIMIT_DOWN  (0)     //回差上限最小值
/* 回差下限上下限 */
#define BACKLASH_DOWN_LIMIT_UP   (0)    //回差下限最大值
#define BACKLASH_DOWN_LIMIT_DOWN (-6)   //回差下限最小值
/* 设定温度上下限 */
#define TEMP_SET_LIMIT_UP       (35)    //设定温度上限
#define TEMP_SET_LIMIT_DOWN     (5)     //设定温度下限
/* 低功耗背光亮度 */
#define LUMINANCE_LOW_POWER     1    

/* 寄存器数据结构 */
typedef struct
{
    uint8_t id;
    uint8_t val;
}Regs;

typedef struct 
{
    Regs     reg[REG_NUM];
    uint8_t  deviceAddr;
    uint8_t  baudrate;
    uint8_t  page;
    uint8_t  luminance;
    uint8_t  lowPower;           //进入低功耗，降低亮度
    uint32_t timer;              //无操作累计时长
    uint8_t  relayMode;  
    uint8_t  net;                //入网
    uint8_t  btnLight[3];        //按键背光
    uint8_t  acStep;             //空调控制状态机
    uint8_t  dnStep;             //地暖控制状态机
    uint32_t speedDelay;
    uint8_t  menuSelected;       //高级模式菜单选中
    uint8_t  menu;           
    uint8_t  fault;              //系统错误标志
    uint8_t  save;               //系统参数更新，需要保存
}Controller;

void set_page(uint8_t page);
void set_device_addr(uint8_t addr);
void set_baudrate(uint8_t baudrate);
void set_luminance(uint8_t luminance);
void set_low_Power(uint8_t state);
void set_relay_mode(uint8_t mode);
void set_net(uint8_t state);
void set_lock(uint8_t state);
void set_system(uint8_t type);
void set_power_ac(uint8_t state);
void set_controller_ac_step(uint8_t step);
void reset_controller_ac(void);
void set_output_speed_delay(uint32_t delay);
void set_ac_mode(uint8_t mode);
void set_speedSet_ac(uint8_t speed);
void set_eco(uint8_t state);
void set_tempSet_ac(uint8_t temp);
void set_temp_upLimit(uint8_t temp);
void set_temp_downLimit(uint8_t temp);
void set_valve_ac(uint8_t state);
void set_valve_dn(uint8_t state);
void set_tempSmp(uint8_t temp);
void set_out_speed_ac(uint8_t speed);
void set_btn_light(uint8_t id, uint8_t state);
void set_menu(uint8_t menu);
void set_calib_unsigned(uint8_t uCalib);
void set_calib_signed(signed char calib);
void set_speed_ctrl(uint8_t state);
void set_low_temp_protect(uint8_t state);
void set_memory_mode(uint8_t state);
void set_backlash_up(uint8_t val);
void set_backlash_down_unsigned(uint8_t uVal);
void set_backlash_down_signed(signed char val);
void set_tempSet_upLimit(uint8_t val);
void set_tempSet_downLimit(uint8_t val);
void set_ac_cool_enable(uint8_t state);
void set_ac_heat_enable(uint8_t state);
void set_ac_air_enable(uint8_t state);
void set_ac_dry_enable(uint8_t state);
void set_machine_enable(uint8_t type);
void set_panel_luminance(uint8_t level);
void set_low_power(uint8_t state);
void set_icon_blink(uint8_t id, uint8_t state);
void set_mode_blink(uint8_t state);
void low_power_process(void);
void set_select_state(uint8_t state);
void set_speed_delay(uint32_t delay);
void set_tempSet_dn(uint8_t temp);
void set_power_dn(uint8_t state);
void set_power_fresh(uint8_t state);
void set_speedSet_fresh(uint8_t speed);
void set_out_speed_fresh(uint8_t speed);
void set_controller_dn_step(uint8_t step);
void reset_controller_dn(void);
void set_unit(uint8_t unit);
void system_wake_up(void);
void set_save(uint8_t state);
void set_fault(uint8_t error);
void sys_params_deinit(void);
void unit_convert(void);
void sys_params_save(void);

uint8_t get_page(void);
uint8_t get_device_addr(void);
uint8_t get_baudrate(void);
uint8_t get_luminance(void);
uint8_t get_low_power_state(void);
uint8_t get_relay_mode(void);
uint8_t get_net_state(void);
uint8_t get_lock_state(void);
uint8_t get_system(void);
uint8_t get_power_ac(void);
uint8_t get_controller_ac_step(void);
uint32_t get_output_speed_delay(void);
uint8_t get_ac_mode(void);
uint8_t get_ac_mode_select(void);
uint8_t get_speedSet_ac(void);
uint8_t get_eco_state(void);
uint8_t get_tempSet_ac(void);
uint8_t get_temp_upLimit(void);
uint8_t get_temp_downLimit(void);
uint8_t get_valve_state_ac(void);
uint8_t get_valve_state_dn(void);
uint8_t get_tempSmp(void);
uint8_t get_out_speed_ac(void);
uint8_t get_btn_light_state(uint8_t id);
uint8_t get_menu(void);
uint8_t get_calib_unsigned(void);
signed char get_calib_signed(void);
uint8_t get_speed_ctrl_state(void);
uint8_t get_low_temp_protect_state(void);
uint8_t get_memory_mode_state(void);
uint8_t get_backlash_up(void);
uint8_t get_backlash_down_unsigned(void);
signed char get_backlash_down_signed(void);
uint8_t get_tempSet_upLimit(void);
uint8_t get_tempSet_downLimit(void);
uint8_t get_ac_cool_enable(void);
uint8_t get_ac_heat_enable(void);
uint8_t get_ac_air_enable(void);
uint8_t get_ac_dry_enable(void);
uint8_t get_machine_enable(void);
uint8_t get_panel_luminance(void);
uint8_t get_selected_state(void);
uint32_t get_speed_delay(void);
uint8_t get_tempSet_dn(void);
uint8_t get_power_dn(void);
uint8_t get_power_fresh(void);
uint8_t get_speedSet_fresh(void);
uint8_t get_out_speed_fresh(void);
uint8_t get_controller_dn_step(void);
uint8_t get_unit(void);
uint8_t get_save(void);
uint8_t get_fault(void);
void get_ctrler_regs(Regs **pReg);
void controller_init(void);
#endif
